package com.xian.gaoxiao.ui.widget;

import java.util.List;

import com.xian.gaoxiao.R;
import com.xian.gaoxiao.adapter.TaskItemAdapter;
import com.xian.gaoxiao.db.TaskDBControl;
import com.xian.gaoxiao.model.TaskItem;
import com.xian.gaoxiao.ui.activity.MainAc;
import com.xian.gaoxiao.utils.TaskConstants;
import com.xian.gaoxiao.utils.Utilities;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ContainerLayout extends LinearLayout implements  OnItemClickListener {

	private static final int MSG_INIT_LISTVIEW = 1;
	private static final int MSG_UPDATE_LISTVIEW = 2;

	private static final int ONCLICK_DURATION = 500;

	private int pos = TaskConstants.STATUS_POS_NONE;

	private Context mContext;
	private View mainView;
	private LinearLayout titleContainer;
	private TextView titleView;
	private ListView listView;
	private TaskItemAdapter adapter;

	private List<TaskItem> mList;

	private TaskDBControl mDBControl;
	
	
	private    long inTime = 0;
	

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case MSG_INIT_LISTVIEW:
				Utilities.print(">>>>MSG_INIT_LISTVIEW>>>>>");
				if (adapter == null) {
					adapter = new TaskItemAdapter(mContext, mList);
					listView.setAdapter(adapter);
					listView.invalidate();
				}
				mHandler.sendEmptyMessage(MSG_UPDATE_LISTVIEW);
				break;

			case MSG_UPDATE_LISTVIEW:
				Utilities.print(">>>>MSG_UPDATE_LISTVIEW>>>>>" + mList.size());
				adapter.notifyDataSetChanged();
				break;

			default:
				break;
			}

		}

	};

	private ContainerLayout[] otherLayouts;

	public ContainerLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public ContainerLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private void init(Context context) {
		mContext = context;
		mainView = View.inflate(mContext, R.layout.item_tap, null);
		this.addView(mainView);
		titleContainer = (LinearLayout) mainView
				.findViewById(R.id.tap_titlecontainer);
		titleView = (TextView) mainView.findViewById(R.id.tap_title);
		listView = (ListView) mainView.findViewById(R.id.tap_listview);
		mDBControl = TaskDBControl.getInstance(mContext);
		listView.setOnItemClickListener(this);

	}

	public void setTapFocus() {
		MainAc.currentState = pos;
		titleContainer.setVisibility(View.VISIBLE);
		listView.setVisibility(View.VISIBLE);
		this.getLayoutParams().height = MainAc.focusHeight;
		this.getLayoutParams().width = MainAc.focusWidth;
		this.setLayoutParams(this.getLayoutParams());

	}

	private void setTapUnFocus(int focusPos) {

		int temp = MainAc.TAP_COUNT - 1 - focusPos;
		if (temp == pos) {
			titleContainer.setVisibility(View.GONE);
			listView.setVisibility(View.GONE);
			this.getLayoutParams().height = MainAc.unFocusHeight;
			this.getLayoutParams().width = MainAc.unFocusWidth;
			this.setLayoutParams(this.getLayoutParams());
		} else {
			temp = pos - focusPos;

			switch (Math.abs(temp)) {
			case 1:
				titleContainer.setVisibility(View.GONE);
				listView.setVisibility(View.GONE);
				this.getLayoutParams().height = MainAc.focusHeight;
				this.getLayoutParams().width = MainAc.unFocusWidth;
				Utilities.print(pos + ":" + MainAc.focusHeight + ":"
						+ MainAc.unFocusWidth);
				this.setLayoutParams(this.getLayoutParams());
				break;
			case 2:
				titleContainer.setVisibility(View.GONE);
				listView.setVisibility(View.GONE);
				this.getLayoutParams().height = MainAc.unFocusHeight;
				this.getLayoutParams().width = MainAc.focusWidth;
				Utilities.print(pos + ":" + MainAc.focusHeight + ":"
						+ MainAc.unFocusWidth);
				this.setLayoutParams(this.getLayoutParams());
				break;

			default:
				break;
			}

		}

	}

	public void setTapInit() {
		titleContainer.setVisibility(View.VISIBLE);
		listView.setVisibility(View.VISIBLE);
		this.getLayoutParams().height = MainAc.commonHeight;
		this.getLayoutParams().width = MainAc.commonWidth;
		this.setLayoutParams(this.getLayoutParams());
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private void validateView(int pos) {
		switch (pos) {
		case TaskConstants.STATUS_POS_LEFTTOP:
			titleView.setText(R.string.text_fd_d1_title);
			this.setBackgroundResource(R.color.c_fd_1);
			break;
		case TaskConstants.STATUS_POS_RIGHTTOP:
			titleView.setText(R.string.text_fd_d2_title);
			this.setBackgroundResource(R.color.c_fd_2);
			break;
		case TaskConstants.STATUS_POS_LEFTBOTTOM:
			titleView.setText(R.string.text_fd_d3_title);
			this.setBackgroundResource(R.color.c_fd_3);
			break;
		case TaskConstants.STATUS_POS_RIGHTBOTTOM:
			titleView.setText(R.string.text_fd_d4_title);
			this.setBackgroundResource(R.color.c_fd_4);
			break;

		default:
			break;
		}
		new Thread(new GetDBData(pos)).start();
	}

	private class GetDBData implements Runnable {
		private int pos;

		GetDBData(int pos) {
			this.pos = pos;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			mList = mDBControl.queryTask(TaskDBControl.WHERE_TASKITEM_BY_RANK,
					new String[] { String.valueOf(pos) });
			mHandler.sendEmptyMessage(MSG_INIT_LISTVIEW);
		}

	}

	public int getPos() {
		return pos;
	}

	public void setPos(int pos) {
		this.pos = pos;
		validateView(pos);
	}

	public ContainerLayout[] getOtherLayouts() {
		return otherLayouts;
	}

	public void setOtherLayouts(ContainerLayout[] otherLayouts) {
		this.otherLayouts = otherLayouts;
	}


	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		Utilities.print("onInterceptTouchEvent>>>" + ev.getAction());
	    if (MainAc.currentState != pos) {
            return true;
        } else {
            return false;
        }

	}
	
	
	
	

	@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
	    Utilities.print("onTouchEvent>>>" + event.getAction());
     

        if (MainAc.currentState != pos) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                inTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                long outTime = System.currentTimeMillis();
                Utilities.print("ONCLICK_DURATION>>>" + (outTime-inTime));
                if ((outTime - inTime) < ONCLICK_DURATION) {
                    setTapFocus();
                    for (int i = 0; i < otherLayouts.length; i++) {
                        if (i != pos) {
                            otherLayouts[i].setTapUnFocus(pos);
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_CANCEL:

                break;

            default:
                break;
            }

            return true;
        } else {
            return false;
        }
        
    }
	
	
	

    @Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

		Utilities.print("onItemClick>>" + arg2);

	}
}
